<{include file="Public/top.html" }>
<script>

    /**
     *兑换编辑页面 初始化 如果兑换的分类为电子卷 则显示流量红包
     */
    $(document).ready(function(){
        //卡券的select value值为2
//        const CART_DEFAULT_VALUE = 2;
//
//
//        var deliveryType = $('#deliver-select').val();
//
//        if(deliveryType == CART_DEFAULT_VALUE){
//            $('#electcard-wrapper').show();
//        }else{
//            $('#electcard-wrapper').hide();
//
//        }

    });

    /**
     * 根据选择不同的分类 显示不同的字段
     * @param obj
     */
    function chooseDeliveryType(obj){
        //卡券的select value值为2
        const CART_DEFAULT_VALUE = 2;
        var deliveryType = $(obj).val();
        if(deliveryType == CART_DEFAULT_VALUE){
            $('#electcard-wrapper').show();
            $('#send-wrapper').hide();
            $("input[name='getWay']").removeAttr("checked");
            $("input[name='add']").removeAttr("checked");
            $("#deliverArea").val('');
            $("#deliverPay").val('');
        }else{
            $('#electcard-wrapper').hide();
            $('#send-wrapper').show();
            $("#express,#pick").css('display','none');
        }
    }

    /**
     * 根据提货方式  显示同字段
     */
    function showMore(e){
        if($(e).val() == 1){//自取
            $('#pick').toggle();
            $("input[name='add']").removeAttr("checked");
        }
        if($(e).val() == 2) {//快递
            $('#express').toggle();
            $("#deliverArea").val('江浙沪');
            $("#deliverPay").val(0);
        }
    }


</script>
</head>
<body xmlns="http://www.w3.org/1999/html">
<div id="warpper">
    <div class="content">
        <div class="location">积分商城</div>
        <{include file= "PointMall/nav.html"}>
        <div class="topic">兑换信息</div>
        <div class="form-box border0 border-box">
            <form action="<{U(<{u($smarty.const.ACTION_NAME)}>)}>" method="post" class="ui-form" id="addManagerForm">
                <div class="ui-form-item inline-block" style="vertical-align:top;">
                    <dl>
                        <label class="ui-label">标题</label>
                        <input type="text" class="ui-text" id="titleInner" name="titleInner" value="<{$obj.titleInner}>" required/>
                    </dl>
                    <dl>
                        <label class="ui-label">列表标题</label>
                        <input type="text" class="ui-text" id="titleList" value="<{$obj.titleList}>" name="titleList" placeholder="不填写时使用内标题" />
                    </dl>
                    <dl>
                        <label class="ui-label">显示排序（降序）</label>
                        <input type="text" value="<{$obj.showOrder}>" class="ui-text" id="showOrder" name="showOrder" required/>
                    </dl>
                    <{if $clientShowSwitch}>
                        <dl>
                            <label class="ui-label">展现渠道</label>
                            <{html_checkboxes name="clientShow" options=$clientShow checked=$selectedClients  separator="&nbsp;"}>
                        </dl>
                    <{/if}>
                    <{if $userLevelSwitch}>
                        <dl>
                            <label class="ui-label">会员等级</label>
                            <input name="userLevelRequire" class="ui-text" value="<{$obj.userLevelRequire|default:0}>"  type="text" placeholder="输入会员等级" />
                        </dl>
                    <{/if}>

                    <dl>
                        <label class="ui-label">积分</label>
                        <input class="ui-text "  value="<{$obj.point}>" name="point" type="text" />
                    </dl>
                    <dl>
                        <label class="ui-label">原价</label>
                        <input type="text" class="ui-text" id="originalPrice" placeholder="单价为：元" name="originalPrice" value="<{$obj.originalPrice}>" required/>
                    </dl>
                    <dl id="now-price" >
                        <label class="ui-label">现价</label>
                        <input type="text" class="ui-text" id="nowPrice" placeholder="单价为：元" name="nowPrice" value="<{$obj.nowPrice}>" required/>
                    </dl>

                    <dl>
                        <label class="ui-label">标签</label>
                        <select name="contentType" class="ui-select">
                            <!--<option value="">美食</option>-->
                            <{html_options options=$contentType selected=$obj.contentType}>
                        </select>
                        <!--<input type="button" id="eventsTypeManage" value="标签管理" class="ui-button"/>-->
                    </dl>
                </div>
                <div class="ui-form-item ui-form-item2 inline-block">
                    <dl>
                        <label class="ui-label">上线时间</label>
                        <input required id="startDate2" name="beginShowTimestamp" value="<{if $obj.beginShowTimestamp gt 0}><{'Y-m-d H:i:s'|date:$obj.beginShowTimestamp}><{/if}>" class="ui-select" type="text" />
                    </dl>
                    <dl>
                        <label class="ui-label">兑换时间</label>
                        <input style="width: 30%" required id="startDate" name="beginExchangeTimestamp" value="<{if $obj.beginExchangeTimestamp gt 0}><{'Y-m-d H:i'|date:$obj.beginExchangeTimestamp}><{/if}>" class="ui-select" type="text" /> - <input style="width: 30%" required id="endDate"  name="endExchangeTimestamp" value="<{if $obj.endExchangeTimestamp gt 0}><{'Y-m-d H:i'|date:$obj.endExchangeTimestamp}><{/if}>" class="ui-select" type="text" />
                    </dl>
                    <div class="ui-form-item2 uploader">
                        <input type="hidden" value="<{$obj.cover}>" id="cover" name="cover">
                        <label class="ui-label">封面图</label>
                        <span class="picture_box picture_270">
                            <div class="a-imgBox" id="imgBox" <{if not $obj.cover}> style="display:none" <{/if}> >
                                    <a href="javascript:;" class="a-close" onclick="deletePic('imgBox','uptxt','cover')"></a>
                                    <img src="<{$obj.cover}>" width="270" height="169" />
                            </div>
                            <div>
                                    <a href="javascript:;" class="a-upload">
                                        <input type="file" name="file" >
                                    </a>
                                    <i class="uptxt" id="uptxt">上传图片</i>
                                    <i>598*332</i>
                            </div>
                        </span>
                    </div>
                    <dl>
                        <div class="ui-form-item2 uploader">
                            <input type="hidden" value="<{$obj.listPic}>" id="listPic" name="listPic">
                            <label class="ui-label">列表图版</label>
                            <span class="picture_box picture_270">
                                <div class="a-imgBox" id="imgBox2" <{if not $obj.cover}> style="display:none" <{/if}> >
                                    <a href="javascript:;" class="a-close" onclick="deletePic('imgBox2','uptxt2','listPic')"></a>
                                    <img src="<{$obj.listPic}>" width="270" height="169" />
                                </div>

                                <div>
                                    <a href="javascript:;" class="a-upload">
                                        <input type="file" name="file" >
                                    </a>
                                    <i class="uptxt" id="uptxt2">上传图片</i>
                                    <i>290*250</i>
                                </div>

                            </span>
                        </div>
                    </dl>
                </div>
                <div class="reply_box">
                    <dl>
                        <label class="ui-label">库存</label>
                        <input class="ui-text " type="text" name="stock" value="<{$obj.stock}>" required/>
                        <label><input type="checkbox" name="isShowStock" value="1" <{if $obj.isShowStock gt 0}>checked='checked'<{/if}>>前台显示库存</label>
                    </dl>
                    <dl>
                        <label class="ui-label">限购数量</label>
                        <input class="ui-text " type="text" name="quota" placeholder="0 表示不限量" value="<{$obj.quota}>" required/>
                    </dl>
                    <dl>
                        <label class="ui-label">销量显示调节</label>
                        <input class="ui-text " type="number" name="salesVolumeAdjust" placeholder="增加总量" value="<{$obj.salesVolumeAdjust}>" required/>
                        <input class="ui-text " type="number" name="salesVolumeAdjustStep" placeholder="单次调节上限" value="<{$obj.salesVolumeAdjustStep}>" required/>
                    </dl>
                </div>
                <div class="reply_box">
                    <label class="ui-label">兑换详情</label>
                    <textarea id="editor" name="content" style="width:730px;display:inline-block;height:400px;margin-top: 10px">
                        <{$obj.content|html_entity_decode}>
                    </textarea>
                </div>
                <div class="reply_box">
                    <label class="ui-label">分享详情</label>
                    <div class="reply_con inline-block">
                        <input type="text" class="ui-text" name="shareDesc" value="<{$obj.shareDesc}>" required/>
                    </div>
                </div>
                <div class="ui-form-item ">
                    <label class="ui-label">要求关注公众号</label>
                    <input type="radio" value="0" id="f0" name="mustFollow" required <{if $obj['mustFollow'] eq 0}>checked<{/if}>/><label for="f0">无要求</label>
                    <input type="radio" value="1" id="f1" name="mustFollow" required <{if $obj['mustFollow'] eq 1}>checked<{/if}>/><label for="f1">必须关注</label>
                </div>
                <div class="reply_box">
                    <label class="ui-label">上线状态选择</label>
                        <input type="radio" id="open" value="1" name="status" <{if $obj.status == 1}> checked <{/if}>>上线</label>
                        <input type="radio" id="close" value="2" name="status" <{if $obj.status == 2}> checked <{/if}> >不上线</label>
                </div>
                <div id="card-day" style="display: none">
                    <div class="ui-form-item">
                        <label class="ui-label">有效期</label>
                        <input type="radio" id="day" name="cardType"  value="1" <{if $card_item.beginTimestamp != null }>checked <{/if}>required><label for="day">&nbsp;&nbsp;固定日期&nbsp;&nbsp;</label>
                        <dl>
                            <label class="ui-label mll00">有效期开始时间</label>
                            <input id="cardDate1" name="beginCardTimestamp" class="ui-select" value="<{$card_item.beginTimestamp}>" type="text" />
                        </dl>
                        <dl>
                            <label class="ui-label mll00">有效期结束时间</label>
                            <input id="cardDate2" name="endCardTimestamp" value="<{$card_item.endTimestamp}>" class="ui-select" type="text" />
                        </dl>
                    </div>
                    <div class="ui-form-item">
                        <label class="ui-label">&nbsp;</label>
                        <input type="radio" id="day2" name="cardType" value="0" <{if $card_item.beginTimestamp == null }>checked <{/if}> required><label for="day2">&nbsp;&nbsp;领取后，&nbsp;&nbsp;</label>
                        <select name="fixedBeginTerm" class="ui-select" style="width:100px;">
                            <option value="0">当天</option>
                        </select>
                        生效，有效天数
                        <select name="fixedTerm" class="ui-select" style="width:100px;">
                            <option value="30">30天</option>
                        </select>
                    </div>
                </div>
                <div class="integral_btn-group">
                    <input type="submit" value="提交" class="ui-button"/>
                </div>
                <input type="hidden" name="id" value="<{$obj.id}>" required/>
            </form>
        </div>
    </div>
</div>
<ul id="AddressEditor" style="display:none;">
    <li class="AddEditorList"><span>默认地址</span>&nbsp;&nbsp;&nbsp;&nbsp;<input class="ui-text" type="text" id="writeAdd1" value="<{$addArr[0]}>" style="width: 70%;"/></li>
    <li class="AddEditorList"><span>临时地址</span>&nbsp;&nbsp;&nbsp;&nbsp;<input class="ui-text" type="text" id="writeAdd2" value="<{$addArr[1]}>" style="width: 70%;"/></li>
</ul>
<{include file="Public/foot.html" }>
<{include file="Public/ueditor.html" }>
<script>

    datePickter('cardDate1','cardDate2');
    datePickter('startDate','endDate');
    datePickter('startDate2','startDate');
    $(document).ready(function(){
        var deliverType = "<{$obj.deliveryType}>";
        var deliverWay = "<{$obj.deliverWay}>";
        var deliverWayArr = deliverWay.split(',');

//        if(deliverType == 1){
//            $("#send-wrapper").css('display','block');
//            if($.inArray('1',deliverWayArr) >= 0){
//                $("#pick").css('display','block');
//            }
//            if($.inArray('2',deliverWayArr) >= 0){
//                $("#express").css('display','block');
//            }
//        }
        $('#deliveryTypeSelect input').each(function(i,e){
            var id=e.value;
            var isShow=e.checked;
            displayDelivery(id,isShow);
        });
    });
    function displayDelivery(id,isShow){
        if(isShow){
            $('.deliver_'+id).show();
        }else{
            $('.deliver_'+id).hide();
        }

        var showPick=false;
        $('#deliveryTypeSelect input').each(function(i,e){
            var id=e.value;
            var isShow=e.checked;
            if ((id==2 || id==4 )&& isShow){
                showPick=true;
            }
        });
        if (showPick){
            $('.deliver_4').show();
        }else{
            $('.deliver_4').hide();
        }


    }

    $('#deliveryTypeSelect input').on('click',function(e){
        var id=e.target.value;
        var isShow=e.target.checked;
        displayDelivery(id,isShow);
    });


    (function ($) {
        'use strict';
        $.fn.uploader = function () {
            return this.each(function () {
                var $this = $(this);
                var $img = $this.find('img');
                var $file = $this.find('input[type=file]');
                var $hidden = $this.find('input[type=hidden]');
                var $txt = $this.find('i.uptxt');
                // var $size = $this.find('span.size');
                /*$img.on('load', function () {
                 var width = $(this).width();
                 var height = $(this).height();
                 $hidden.val($img.attr('src'));
                 $size.text(width + 'x' + height);
                 });*/
                var $imgType = $this.find('label').html();
                var from = null;
                if($imgType == '封面图'){
                    from = "IMG_POINT_EXCHANGE_INFO,IMG_ARTICLE_INFO_ABOUT";
                }
                if($imgType == '列表图版'){
                    from = "IMG_POINT_EXCHANGE_LIST"
                }
                $file.AjaxFileUpload({
                    action: './api.php?m=Api&c=index&a=ajaxUpload&from='+from,
                    onSubmit: function () {
                        $txt.text('上传中');
                    },
                    onComplete: function (filename, response) {
                        if(parseInt(response.id)>0){
                            $txt.text("上传成功");
                            $hidden.attr('value', response.msg);
                            $img.attr("src",response.msg);
                            if($imgType == '封面图'){
                                $('#imgBox').show();
                            }else{
                                $('#imgBox2').show();
                            }
                        }else{
                            $txt.text(response.msg);
                        }

                    }
                });
            });
        }
    })(jQuery);

    $('.uploader').uploader();

    function deletePic(box,i,hidden) {
        $('#'+box).hide();
        $('#'+i).text('上传图片');
        $('#'+hidden).val('');
    }

    function pickThisAddress(obj){
        $('#pickAddress').val(obj.value);
    }

    function editAddress(){
        var AddressEditor = dialog({
            title: '自提地址编辑',
            width: 300,
            content: $("#AddressEditor"),
            onshow: function(){
                $("#AddressEditor form")[0].reset();
            },
            button: [
                {
                    value: '保存',
                    callback: function () {
                        var add1 = $('#writeAdd1').val();
                        var add2 = $('#writeAdd2').val();
                        var param = {add1:add1,add2:add2};

                        $.post("<{U('PointMall/savePickAddress')}>",param,function(res){
                            layer.msg(res.msg);
                            if(res.status == 0){
                                $("#pick").empty().append(res.dataHTML);
                            }
                        });
                    },
                    autofocus: true
                },
                {
                    value: '取消'
                }
            ]
        });
        AddressEditor.showModal();
    }

    $("form").submit(function () {
        if( !$("#listPic").val() || !$("#cover").val()){
            layer.msg('请上传图片！封面图及列表图都是必需的');
            return false;
        }
        if( !UE.getEditor('editor').getContent() ){
            layer.msg('请填写文章详情！');
            return false;
        }
        $("#deliverWay").val($("input[name='getWay']:checked").map(function(index,elem) {return $(elem).val();}).get().join(','));
        if($('#deliver-select').val() == 1 && $("#deliverWay").val()==""){
            layer.msg("请选择提货方式");
            return false;
        }
        layer.msg("正在提交数据...",{time:999});
        $.post("<{U(<{u($smarty.const.ACTION_NAME)}>)}>",$(this).serialize(), function(data){
            layer.closeAll();
            if(data.status==0){
                layer.msg(data.info);
            }else{
                layer.msg(data.info);
                location.href="<{U('online')}>";
            }
        }, 'json');
        return false;
    });
</script>